﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Interface;
using LY.Entity;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Entity.Jsons;

namespace QJ.CMS.Areas.Admin.Controllers
{
    public class DeptController : Controller
    {
        //
        // GET: /Dept/
        private IDeptService _dservice;
        public DeptController(IDeptService dService)
        {
            _dservice = dService;
        }
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult DeptList(FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<LY.Entity.Entities.S_Dept_EX> pList = _dservice.QueryDeptEXList(view);
            JsonQTable fdata = JsonQTable.ConvertFromPagedList<LY.Entity.Entities.S_Dept_EX>(pList, colkey, colsinfo.Split(','));
            return Json(fdata);
        }

        //编辑部门
        public ActionResult DeptEdit(int? id)
        {
            S_Dept dept = null;
            if (id.HasValue)
            {
                dept = _dservice.GetDept(id.Value);
            }
            else
            {
                dept = new S_Dept();
            }
            return View(dept);
        }

        [HttpPost]
        public JsonResult DeptSave(int? id, S_Dept dept, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            int ParentID = 0;
            string path = "";
            int Sequence = 0;
            if (collection["DepartMent"] != null || collection["DepartMent"] != "")
            {
                ParentID = Convert.ToInt32(collection["DepartMent"]);
                S_Dept parentDept = new S_Dept();
                parentDept = _dservice.GetDept(ParentID);
                if (collection["Sequence"] != null && collection["Sequence"] != "")
                {
                    Sequence = Convert.ToInt32(collection["Sequence"]);
                }
                if (parentDept.ParentID == 0)
                {
                    path = "," + (parentDept.ID + 100000000 + Sequence * 10000).ToString() + ",";
                }
                else
                {
                    path = parentDept.Path + (parentDept.ID + 100000000 + Sequence * 10000).ToString() + ",";
                }
            }

            dept.Sequence = Sequence;
            dept.ParentID = ParentID;
            dept.Path = path;
            dept.IsStop = 0;
            try
            {
                string errmsg = "";
                if (!Validate(dept, out errmsg))
                {
                    rMsg.Status = -1;
                    rMsg.Message = errmsg;
                    return Json(rMsg);
                }
                //判断部门标号是否已存在
                int deptid = 0;
                if (dept.ID != null)
                {
                    deptid = dept.ID;
                }
                int count = _dservice.SelectCodeCount(deptid,dept.Code);
                if (count >= 1)//已存在相同的部门标号
                {
                    rMsg.Status = -1;
                    rMsg.Message = "已有相同的编号";
                }
                else
                {
                    int ret = _dservice.SaveDept(dept);
                    if (ret > 0)
                    {
                        rMsg.Status = 1;
                        rMsg.Data = ret;
                    }
                    else
                    {
                        rMsg.Status = -1;
                        rMsg.Message = "意外错误，请稍后重试！";
                    }
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        private static bool Validate(S_Dept dept, out string errmsg)
        {
            errmsg = "";
            if (dept == null)
            {
                errmsg = "参数错误！";
                return false;
            }
            if (string.IsNullOrEmpty(dept.Name))
            {
                errmsg = "部门名称不能为空！";
                return false;
            }

            return true;
        }


        [HttpPost]
        public ActionResult DeptDelete(int id, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                    return Json(rMsg);
                }

                int ret = _dservice.DeleteDept(id);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

    }
}
